
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 定义菜品结构体
typedef struct {
    int id;
    char name[50];
    char category[50];
    float price;
    char introduction[100];
} Dish;

// 定义餐桌号结构体
typedef struct {
    int id;
    int capacity;
} DiningTable;

// 定义用餐者结构体
typedef struct {
    int id;
    int tableNum;
    char phone[20];
    char name[50];
    // 其他用餐信息如用餐日期等可按需添加
} User;

// 菜品设置相关函数
void setM(Dish *dishes, int *count, int max) {
    if (*count >= max) {
        printf("菜品数量已达上限，无法添加新菜品\n");
        return;
    }
    Dish newDish;
    printf("请输入菜品编号: ");
    scanf("%d", &newDish.id);
    printf("请输入菜品名称: ");
    scanf("%s", newDish.name);
    printf("请输入菜品类别: ");
    scanf("%s", newDish.category);
    printf("请输入菜品价格: ");
    scanf("%f", &newDish.price);
    printf("请输入菜品简介: ");
    scanf("%s", newDish.introduction);
    dishes[*count] = newDish;
    (*count)++;
}

// 用餐桌号设置相关函数
void setDT(DiningTable *tables, int *count, int max) {
    if (*count >= max) {
        printf("餐桌号数量已达上限，无法添加新餐桌号\n");
        return;
    }
    DiningTable newTable;
    printf("请输入餐桌编号: ");
    scanf("%d", &newTable.id);
    printf("请输入可就餐人数: ");
    scanf("%d", &newTable.capacity);
    tables[*count] = newTable;
    (*count)++;
}

// 管理用户查询功能
void adminS(Dish *dishes, int dishCount, DiningTable *tables, int tableCount, User *users, int userCount) {
    int choice;
    printf("1. 按菜品查询\n2. 按桌号查询\n3. 按用餐者电话查询\n4. 按用餐者姓名查询\n5. 按日期查询（暂未实现）\n请选择查询方式: ");
    scanf("%d", &choice);
    switch (choice) {
        case 1: {
            int dishId;
            printf("请输入菜品编号: ");
            scanf("%d", &dishId);
            for (int i = 0; i < dishCount; i++) {
                if (dishes[i].id == dishId) {
                    printf("菜品名称: %s，类别: %s，价格: %.2f，简介: %s\n", dishes[i].name, dishes[i].category, dishes[i].price, dishes[i].introduction);
                    return;
                }
            }
            printf("未找到该菜品\n");
            break;
        }
        case 2: {
            int tableId;
            printf("请输入餐桌编号: ");
            scanf("%d", &tableId);
            for (int i = 0; i < tableCount; i++) {
                if (tables[i].id == tableId) {
                    printf("餐桌可就餐人数: %d\n", tables[i].capacity);
                    return;
                }
            }
            printf("未找到该餐桌号\n");
            break;
        }
        case 3: {
            char phone[20];
            printf("请输入用餐者电话: ");
            scanf("%s", phone);
            for (int i = 0; i < userCount; i++) {
                if (strcmp(users[i].phone, phone) == 0) {
                    printf("用餐者姓名: %s，桌号: %d\n", users[i].name, users[i].tableNum);
                    return;
                }
            }
            printf("未找到该用餐者\n");
            break;
        }
        case 4: {
            char name[50];
            printf("请输入用餐者姓名: ");
            scanf("%s", name);
            for (int i = 0; i < userCount; i++) {
                if (strcmp(users[i].name, name) == 0) {
                    printf("用餐者电话: %s，桌号: %d\n", users[i].phone, users[i].tableNum);
                    return;
                }
            }
            printf("未找到该用餐者\n");
            break;
        }
        default:
            printf("无效的查询方式\n");
    }
}

// 管理用户统计功能（简单实现日营业额统计）
void adminT(Dish *dishes, int dishCount, User *users, int userCount) {
    float totalRevenue = 0;
    // 这里假设每个用户点的菜都是固定价格，实际需根据点餐逻辑计算
    for (int i = 0; i < userCount; i++) {
        for (int j = 0; j < dishCount; j++) {
            // 假设每个用户都点了所有菜品，实际应根据点餐情况判断
            totalRevenue += dishes[j].price;
        }
    }
    printf("今日营业额: %.2f\n", totalRevenue);
}

// 用餐者登录功能
void uesrREG(User *users, int *count, int max) {
    if (*count >= max) {
        printf("用户数量已达上限，无法注册新用户\n");
        return;
    }
    User newUser;
    printf("请输入桌号（3位整数）: ");
    scanf("%d", &newUser.tableNum);
    printf("请输入电话号码: ");
    scanf("%s", newUser.phone);
    printf("请输入姓名: ");
    scanf("%s", newUser.name);
    newUser.id = *count + 1;
    users[*count] = newUser;
    (*count)++;
}

// 用餐者查询及点餐功能
void userOR(User user, Dish *dishes, int dishCount) {
    printf("欢迎用餐者 %s，您的桌号是 %d\n", user.name, user.tableNum);
    printf("菜品列表:\n");
    for (int i = 0; i < dishCount; i++) {
        printf("%d. %s - %s - %.2f\n", dishes[i].id, dishes[i].name, dishes[i].category, dishes[i].price);
    }
    // 这里可添加点餐逻辑，如选择菜品编号等
}

// 用餐者结账功能
void userPB(User user) {
    printf("用餐者 %s，桌号 %d 结账功能（暂未实现具体结账逻辑）\n", user.name, user.tableNum);
}

int main(int argc, char *argv[]) {
    if (argc != 3) {
        printf("参数错误，正确格式为: %s <可执行程序名称> <-admin| - user>\n", argv[0]);
        return 1;
    }
    const int MAX_DISHES = 100;
    const int MAX_TABLES = 50;
    const int MAX_USERS = 200;
    Dish dishes[MAX_DISHES];
    DiningTable tables[MAX_TABLES];
    User users[MAX_USERS];
    int dishCount = 0, tableCount = 0, userCount = 0;

    if (strcmp(argv[2], "-admin") == 0) {
        int choice;
        while (1) {
            printf("管理员操作:\n1. 菜品设置\n2. 用餐桌号设置\n3. 查询功能\n4. 统计功能\n5. 退出\n请选择操作: ");
            scanf("%d", &choice);
            switch (choice) {
                case 1:
                    setM(dishes, &dishCount, MAX_DISHES);
                    break;
                case 2:
                    setDT(tables, &tableCount, MAX_TABLES);
                    break;
                case 3:
                    adminS(dishes, dishCount, tables, tableCount, users, userCount);
                    break;
                case 4:
                    adminT(dishes, dishCount, users, userCount);
                    break;
                case 5:
                    return 0;
                default:
                    printf("无效的选择\n");
            }
        }
    } else if (strcmp(argv[2], "-user") == 0) {
        uesrREG(users, &userCount, MAX_USERS);
        userOR(users[userCount - 1], dishes, dishCount);
        userPB(users[userCount - 1]);
    } else {
        printf("无效的用户身份\n");
    }
    return 0;
}